package com.bric.cacheFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;

import com.bric.util.StaticStr;

public class HeaderFile extends Thread{
	private static final Logger log=Logger.getLogger(HeaderFile.class);
	@Override
	public void run() {
		File header=new File(StaticStr.localFilePath+StaticStr.headerPath);
		if(header.exists()&&header.isDirectory()){
			File[] files=header.listFiles();
			for(File file:files){
				copy2FtpPath(StaticStr.finalFtpRoot+StaticStr.finalFileRoot,file);
			}
		}
		
	}
	private void copy2FtpPath(String ftpPath, File file) {
		File targetDir=new File(ftpPath);
		File targetFile=new File(ftpPath+"/"+file.getName());
		log.info("文件地址是"+ftpPath+file.getName());
		if(!targetDir.exists()){
			targetDir.mkdirs();
		}
		log.info(targetFile.getAbsolutePath());
		try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(targetFile));BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file))){
			byte[] bytes=new byte[1024];
			int len=0;
			while((len=bis.read(bytes))!=-1){
				bos.write(bytes, 0, len);
			}
			log.info("写入文件成功");
		} catch (Exception e) {
			log.info("读写文件出现异常"+e.getMessage());
		}
	}
}
